﻿package net.akkz.server.impl;

import net.akkz.dao.StudentDao;
import net.akkz.dbFactory.DbManage;
import net.akkz.dbFactory.Session;
import net.akkz.exception.SQLExcuteException;
import net.akkz.model.Student;
import net.akkz.server.StringCheck;
import net.akkz.server.StudentServer;

public class StudentServerImpl implements StudentServer
{
	private StudentDao studentDao;
	private DbManage dbManage;
	private StringCheck stringCheck;

	@Override
	public Student active(Student student)
	{
		Session session = dbManage.createSession();

		try
		{
			Student studentDb = studentDao.getStudent(session, student.getId());

			if (studentDb.getName().equals(student.getName()) && studentDb.getId() == student.getId())
			{
				studentDao.activeStudent(session, student.getId());
			}

			session.commit();

			return studentDb;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public boolean changePassword(int studentId, String password, String passwordConform)
	{
		if (password.equals(passwordConform))
		{
			Session session = dbManage.createSession();

			try
			{
				Student student = studentDao.getStudent(session, studentId);

				student.setPassword(password);

				studentDao.updateStudent(session, student);

				session.commit();

				return true;
			}
			catch (Exception e)
			{
				session.rollback();
			}
		}

		return false;
	}

	@Override
	public void changeEmail(int studentId, String email)
	{
		Session session = dbManage.createSession();

		try
		{
			Student student = studentDao.getStudent(session, studentId);

			student.setMail(email);

			studentDao.updateStudent(session, student);

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}
	}

	@Override
	public Student login(Student student)
	{
		Session session = dbManage.createSession();

		try
		{
			if(!stringCheck.checkPassword(student.getPassword()))
			{
				student.setName("Password illegal");
				
				return student;
			}
			
			if(!stringCheck.checkLength(student.getName(), 2, 10))
			{
				student.setName("Username length illegal");
				
				return student;
			}
			
			Student dbStudent = studentDao.getStudent(session, student.getName());

			session.commit();

			if (dbStudent.getPassword().equals(student.getPassword()))
			{
				return dbStudent;
			}
			else
			{
				student.setName("Password Error");
				
				return student;
			}
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public Student register(String name, String password, String passwordConform, String mail)
	{
		Session session = dbManage.createSession();
		
		try
		{
			Student student = new Student();
			student.setId(0);
			
			if(!(stringCheck.checkPassword(password) && stringCheck.checkPassword(passwordConform)))
			{
				student.setName("Password illegal");
				
				return student;
			}
			
			if(!stringCheck.checkMail(mail))
			{
				student.setName("Mail illegal");
				
				return student;
			}
			
			if(!stringCheck.checkLength(name, 2, 10))
			{
				student.setName("Username length illegal");
				
				return student;
			}
			
			if(password.equals(passwordConform))
			{
				student.setName(name);
				student.setPassword(password);
				student.setMail(mail);
				student.setState(1);
				
				studentDao.register(session, student);
				
				student = studentDao.getStudent(session, student.getName());
				
				session.commit();
				
				return student;
			}
			else
			{
				student.setName("Password are not same as PasswordConform");
				
				return student;
			}
		}
		catch (SQLExcuteException e)
		{
			session.rollback();
			
			Student student = new Student();
			student.setName("Username has been used");
			
			return student;
		}
		catch (Exception e)
		{
			session.rollback();
			
			e.printStackTrace();
		}

		return null;
	}
	

	@Override
	public Student get(int studentid)
	{
		Session session = dbManage.createSession();

		try
		{
			Student student = studentDao.getStudent(session, studentid);

			session.commit();
			
			return student;
		}
		catch (Exception e)
		{
			session.rollback();
		}
		
		return null;
	}

	public void setStudentDao(StudentDao studentDao)
	{
		this.studentDao = studentDao;
	}

	public void setDbManage(DbManage dbManage)
	{
		this.dbManage = dbManage;
	}

	public void setStringCheck(StringCheck stringCheck)
	{
		this.stringCheck = stringCheck;
	}

}
